<template>
	<view>
		<view class="header">
			<u-swiper :list="imgs" height="100%" interval='3000' mode='rect' border-radius='0'></u-swiper>
			<u-navbar :border-bottom='false' :background='background' back-icon-color='#333333' back-icon-size='48'>
				<view class="searchBar">
					<u-icon class="icon" name="search" color="#666666" size="32"></u-icon>
					<input type="text" v-model="keyword" placeholder=""
						placeholder-style="color:#666666;font-size:28rpx" />
					<view class="text" @click="search">
						搜索
					</view>
				</view>
			</u-navbar>
		</view>
		<view class="tab-bar">
			<u-tabs :list="tabList" :is-scroll="false" :current="current" @change="tabChange" bg-color='#F2F2F2'
				active-color='#333333' inactive-color='#333333' bar-height='8' bar-width='80' :bar-style='barStyle'
				gutter='60'></u-tabs>
		</view>
		<view class="content" v-show="current==0">
			<u-waterfall v-model="goodsList" ref="tcWaterfall">
				<template v-slot:left="{leftList}">
					<view class="goods" v-for="(item, index) in leftList" :key="index"
						@click="navTo('./goods/goods?id='+item.goods_sku_id)">
						<image :src="item.image_path" mode=""></image>
						<view class="title">
							{{item.name}}
						</view>
						<view class="price">
							<view class="tit">
								<text style="font-weight: normal;font-size: 24rpx;">¥</text>{{item.i_price}}
							</view>
						<!-- 	<view class="tit" v-else>
								<text style="font-weight: normal;font-size: 24rpx;">¥</text>{{item.i_vip_price}}
							</view> -->
							<text class="fukuan">{{item.sales_sum}}人付款</text>
						</view>
						<view class="price-box">
							<view class="rel-price">
								¥<text>{{Number(item.i_price)-Number(item.use_welfare_fund)}}</text>
							</view>
						<!-- 	<view class="rel-price" v-else>
								¥<text>{{Number(item.i_vip_price)-Number(item.use_welfare_fund)}}</text>
							</view> -->
							<view class="fl-price">
								+ {{item.use_welfare_fund}} 福利金
							</view>
						</view>
					</view>
				</template>
				<template v-slot:right="{rightList}">
					<view class="goods" v-for="(item, index) in rightList" :key="index"
						@click="navTo('./goods/goods?id='+item.goods_sku_id)">
						<image :src="item.image_path" mode=""></image>
						<view class="title">
							{{item.name}}
						</view>
						<view class="price">
							<view class="tit">
								<text style="font-weight: normal;font-size: 24rpx;">¥</text>{{item.i_price}}
							</view>
							<!-- <view class="tit" v-else>
								<text style="font-weight: normal;font-size: 24rpx;">¥</text>{{item.i_vip_price}}
							</view> -->
							<text class="fukuan">{{item.sales_sum}}人付款</text>
						</view>
						<view class="price-box">
							<view class="rel-price">
								¥<text>{{Number(item.i_price)-Number(item.use_welfare_fund)}}</text>
							</view>
						<!-- 	<view class="rel-price" v-else>
								¥<text>{{Number(item.i_vip_price)-Number(item.use_welfare_fund)}}</text>
							</view> -->
							<view class="fl-price">
								+ {{item.use_welfare_fund}} 福利金
							</view>
						</view>
					</view>
				</template>
			</u-waterfall>
		</view>

		<view class="content" v-show="current==1">
			<u-waterfall v-model="jpGoodsList" ref="jpWaterfall">
				<template v-slot:left="{leftList}">
					<view class="goods" v-for="(item, index) in leftList" :key="index"
						@click="navTo('./goods/goods?id='+item.goods_sku_id)">
						<image :src="item.image_path" mode=""></image>
						<view class="title">
							{{item.name}}
						</view>
						<view class="price">
							<view class="tit">
								<text style="font-weight: normal;font-size: 24rpx;">¥</text>{{item.i_price}}
							</view>
					<!-- 		<view class="tit" v-else>
								<text style="font-weight: normal;font-size: 24rpx;">¥</text>{{item.i_vip_price}}
							</view> -->
							<text class="fukuan">{{item.sales_sum}}人付款</text>
						</view>
						<view class="price-box">
							<view class="rel-price">
								¥<text>{{Number(item.i_price)-Number(item.use_welfare_fund)}}</text>
							</view>
						<!-- 	<view class="rel-price" v-else>
								¥<text>{{Number(item.i_vip_price)-Number(item.use_welfare_fund)}}</text>
							</view> -->
							<view class="fl-price">
								+ {{item.use_welfare_fund}} 福利金
							</view>
						</view>
					</view>
				</template>
				<template v-slot:right="{rightList}">
					<view class="goods" v-for="(item, index) in rightList" :key="index"
						@click="navTo('./goods/goods?id='+item.goods_sku_id)">
						<image :src="item.image_path" mode=""></image>
						<view class="title">
							{{item.name}}
						</view>
						<view class="price">
							<view class="tit">
								<text style="font-weight: normal;font-size: 24rpx;">¥</text>{{item.i_price}}
							</view>
						<!-- 	<view class="tit" v-else>
								<text style="font-weight: normal;font-size: 24rpx;">¥</text>{{item.i_vip_price}}
							</view> -->
							<text class="fukuan">{{item.sales_sum}}人付款</text>
						</view>
						<view class="price-box">
							<view class="rel-price">
								¥<text>{{Number(item.i_price)-Number(item.use_welfare_fund)}}</text>
							</view>
						<!-- 	<view class="rel-price" v-else>
								¥<text>{{Number(item.i_vip_price)-Number(item.use_welfare_fund)}}</text>
							</view> -->
							<view class="fl-price">
								+ {{item.use_welfare_fund}} 福利金
							</view>
						</view>
					</view>
				</template>
			</u-waterfall>
		</view>
		<u-back-top :scroll-top="scrollTop"></u-back-top>
	</view>
</template>

<script>
	import myRequest from '@/api/index.js'
	export default {
		data() {
			return {
				scrollTop:0,
				type: 1,
				keyword: '',
				current: 0,
				tabList: [{
					name: '特产'
				}, {
					name: '精品'
				}],
				imgs: [],
				background: {
					backgroundColor: 'rgba(242, 242, 247, 0)'
				},
				barStyle: {
					background: 'linear-gradient(180deg, #3FA3F9 0%, #2D7BF7 100%)',
					'border-radius': '40rpx'
				},
				goodsList: [],
				jpGoodsList: [],
				page: '1',
				loadStatus: 'loading',
			}
		},
		onPageScroll(e) {
			this.scrollTop = e.scrollTop
			if (e.scrollTop > 144) {
				this.background.backgroundColor = 'rgba(242, 242, 247, 1)'
			} else {
				this.background.backgroundColor = 'rgba(242, 242, 247, 0)'
			}
		},
		onLoad() {
			this.getTcGoods()
			this.getBanners()
		},
		onReachBottom() {
			let page = Number(this.page) + 1
			this.page = page.toString()
			this.getTcGoods()
			this.getJpGoods()
			setTimeout(() => {
				if (!this.isLoading) {
					this.loadStatus = 'nomore'
					this.isLoading = true
				} else {
					this.loadStatus = 'loading';
				}
			}, 1000)
		},
		methods: {
			async getBanners() {
				let res = await myRequest.getBanner({
					position: '3'
				})
				if (res.status) {
					res.data.forEach(item => {
						this.imgs.push(item.pic_url)
					})
				}
			},
			search() {
				this.page = '1'
				this.$refs.tcWaterfall.clear()
				this.getTcGoods()
			},
			// 特产
			async getTcGoods() {
				let {
					page,
					keyword,
					type
				} = this
				let res = await myRequest.getGoodsList({
					page: page,
					goods_source: '2',
					keyword: keyword ? keyword : '',
					category_type: type
				})
				if (res.status) {
					if (res.data.length == 0) {
						this.isLoading = false
						return
					} else {
						this.goodsList = [...this.goodsList, ...res.data]
					}
				}
			},
			// 精品
			async getJpGoods() {
				let {
					page,
					keyword,
					type
				} = this
				let res = await myRequest.getGoodsList({
					page: page,
					goods_source: '2',
					keyword: keyword ? keyword : '',
					category_type: type
				})
				if (res.status) {
					if (res.data.length == 0) {
						this.isLoading = false
						return
					} else {
						this.jpGoodsList = [...this.jpGoodsList, ...res.data]
					}
				}
			},

			tabChange(index) {
				this.current = index
				this.page = '1'
				this.$refs.jpWaterfall.clear()
				this.$refs.tcWaterfall.clear()
				if (this.current == 0) {
					this.type = 1
					this.getTcGoods()
				}
				if (this.current == 1) {
					this.type = 2
					this.getJpGoods()
				}
			},
			navTo(url) {
				uni.navigateTo({
					url: url
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	/deep/.u-swiper-wrap {
		width: 100%;
		height: 100%;
	}

	.header {
		width: 100%;
		height: 400rpx;

		.searchBar {
			display: flex;
			align-items: center;

			input {
				width: 618rpx;
				height: 64rpx;
				background: #FFFFFF;
				border-radius: 40rpx;
				text-indent: 64rpx;
				font-size: 28rpx;
			}

			.icon {
				position: absolute;
				left: 108rpx;
				z-index: 88;
			}

			.text {
				width: 88rpx;
				height: 64rpx;
				position: absolute;
				z-index: 88;
				right: 46rpx;
				font-size: 28rpx;
				line-height: 64rpx;
				font-family: PingFang SC;
				font-weight: 400;
				color: #3587F7;
			}
		}
	}

	.tab-bar {
		width: 42%;
	}

	.content {
	padding: 0 15rpx;

		.goods {
			width: 348rpx;
			height: auto;
			background-color: #FFFFFF;
			border-radius: 16rpx;
			padding-bottom: 18rpx;
			overflow: hidden;
			margin-top: 20rpx;

			image {
				width: 348rpx;
				height: 348rpx;
			}

			.price-box {
				padding-left: 10rpx;
				width: fit-content;
				width: -webkit-fit-content;
				width: -moz-fit-content;
				display: flex;
				margin-top: 12rpx;

				.rel-price {
					height: 44rpx;
					width: fit-content;
					width: -webkit-fit-content;
					width: -moz-fit-content;
					background: linear-gradient(180deg, #FBE9B9 0%, #FDE6B8 100%);
					border-radius: 8rpx 0px 0px 8rpx;
					padding: 0 6rpx;
					font-size: 24rpx;
					font-family: Arial;
					font-weight: 400;
					line-height: 44rpx;
					color: #333333;

					text {
						font-size: 32rpx;
						font-weight: bold;
						margin-left: 4rpx;
					}
				}

				.fl-price {
					height: 44rpx;
					width: fit-content;
					width: -webkit-fit-content;
					width: -moz-fit-content;
					background: #333333;
					border-radius: 0px 8rpx 8rpx 0px;
					font-size: 24rpx;
					font-family: Arial;
					font-weight: 400;
					color: #FBE8B8;
					line-height: 44rpx;
					padding: 0 6rpx;
				}
			}

			.title {
				margin-top: 6rpx;
				padding-left: 12rpx;
				font-size: 30rpx;
				font-family: PingFang SC;
				font-weight: 400;
				color: #333333;
			}

			.price {
				margin-top: 6rpx;
				padding-left: 12rpx;
				padding-right: 12rpx;
				display: flex;
				align-items: center;

				.tit {
					font-size: 36rpx;
					font-family: Arial;
					font-weight: 600;
					color: #FF4600;
					text-decoration: line-through
				}

				.fukuan {
					margin-left: auto;
					font-size: 24rpx;
					font-family: PingFang SC;
					font-weight: 400;
					color: #999999;
				}
			}
		}
	}
</style>
